package com.xiaomage.view.draw6

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.view.View
import android.view.animation.LinearInterpolator

/**
 * Created by Administrator on 2018/1/2.
 */
class MyView4(context: Context): View(context) {
    private var bochang=400f
    private var setY=300f
    private var dx=0f
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val paint= Paint()
        paint.style= Paint.Style.FILL
        paint.isAntiAlias=true
        paint.color= Color.RED

        val path= Path()
        //向左移动一个波长
        path.moveTo(-bochang+dx,setY)
        var i=-bochang;
        while (i<=width+bochang){
            path.rQuadTo(bochang/4,-100f,bochang/2,0f)
            path.rQuadTo(bochang/4,100f,bochang/2,0f)
            i+=bochang
        }
        path.lineTo(width.toFloat(),height.toFloat())
        path.lineTo(0f,height.toFloat())
        path.close()
        canvas.drawPath(path,paint)
    }

    public fun startAnimation(){
        val valueAnimator=ValueAnimator.ofFloat(0f,bochang)
        valueAnimator.duration=500
        valueAnimator.interpolator=LinearInterpolator()
        valueAnimator.repeatCount=ValueAnimator.INFINITE
        valueAnimator.addUpdateListener {
            animation->
            dx=animation.animatedValue.toString().toFloat()
            postInvalidate()
        }
        valueAnimator.start()
    }
}